#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::Cfg_Sql_Init()
{
    ui->tw_cont_sql->setModel(SqlMode);
    ui->tw_cont_sql->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);   // 行拉伸
    ui->tw_cont_sql->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);     // 列拉伸
    ui->tw_cont_sql->verticalHeader()->setVisible(false);    // 行表头不可见
}
// 增
void MainWindow::on_btn_add_sql_clicked()
{
    // 获得表的行数
    int rowNum = SqlMode->rowCount();
    // 添加一行
    SqlMode->insertRow(rowNum);
    SqlMode->setData(SqlMode->index(rowNum,0),rowNum);
}
// 删
void MainWindow::on_btn_del_sql_clicked()
{
    int CurRow = ui->tw_cont_sql->currentIndex().row();
    if(CurRow >= 0)
    {
        // 删除
        SqlMode->removeRow(CurRow);
        // 提交
        SqlMode->submitAll();
        //SqlMode->revertAll(); //撤销
    }
    else
    {
        SqlMode->removeRows(0, SqlMode->rowCount());
        // 提交
        SqlMode->submitAll();
    }
}
// 改
void MainWindow::on_btn_upd_sql_clicked()
{
    // 开始事务操作
   SqlMode->database().transaction();
   if (SqlMode->submitAll())
   {
       SqlMode->database().commit(); //提交
   }
   else
   {
       SqlMode->database().rollback(); //回滚
   }
}
// 查
void MainWindow::on_btn_sel_sql_clicked()
{
    QString SqlKey = ui->le_sel_sql->text();
    if(SqlKey.isEmpty())
    {
        SqlMode->setTable("configure");
        SqlMode->select();
    }
    else
    {
        SqlMode->setFilter(QString("name like '%1'").arg(SqlKey));
        SqlMode->select();
    }
}
// 撤销操作
void MainWindow::on_btn_ret_sql_clicked()
{
    SqlMode->revertAll();
    SqlMode->setTable("configure");
    SqlMode->select();
}
